package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Charge;
import cn.wolfcode.crm.domain.History;
import cn.wolfcode.crm.domain.Paymen;
import cn.wolfcode.crm.domain.Task;
import cn.wolfcode.crm.mapper.*;
import cn.wolfcode.crm.query.PaymenQueryObject;
import cn.wolfcode.crm.query.TaskQueryObject;
import cn.wolfcode.crm.service.IChargeService;
import cn.wolfcode.crm.service.IPaymenService;
import cn.wolfcode.crm.service.ITaskService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.krb5.internal.PAData;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/6/12.
 */

@Service
public class PaymenServiceImpl implements IPaymenService {

    @Autowired
    private PaymenMapper paymenMapper;
    @Autowired
    private ChargeMapper chargeMapper;
    @Autowired
    private HistoryMapper historyMapper;


    @Override
    public PageResult query(PaymenQueryObject qo) {
        int count = paymenMapper.query4Count(qo);
        if (count > 0) {
            return new PageResult(count, paymenMapper.query4List(qo));
        }
        return new PageResult(count, Collections.EMPTY_LIST);
    }

    @Override
    public void pay(Paymen paymen) {
        //生成时间毫毛数对象
        Calendar calendar = Calendar.getInstance();
        //获取对象信息封装到收费查询
        Charge charge = new Charge();
        charge.setChargeSn(calendar.getTime().getTime()+"");
        charge.setClient(paymen.getClient());
        charge.setSn(paymen.getSn());
        charge.setMoney(paymen.getMoney());
        charge.setStatus(true);
        charge.setWay(paymen.getWay());
        charge.setStartDate(paymen.getStartDate());
        charge.setEndDate(paymen.getEndDate());
        charge.setOrganization(paymen.getOrganization());
        charge.setEmployee(UserContext.getCurrentUser());
        charge.setProduct(paymen.getProduct());
        chargeMapper.insert(charge);
        //隐藏缴费处理
        paymen.setStatus(true);
        paymenMapper.updateStatus(paymen);
        //修改单据历史记录的状态设置成为已缴费状态
        History history = historyMapper.selectByNum(paymen.getSn());
        history.setState(5L);
        history.setBeginDate(paymen.getStartDate());
        history.setCheckDate(paymen.getStartDate());
        history.setEndDate(paymen.getEndDate());
        historyMapper.updateSta(history);
    }

    @Override
    public List<Paymen> selectAll() {
        return paymenMapper.selectAll();
    }

}
